package com.car.advertising.activity;

import android.Manifest;
import android.app.AppOpsManager;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import com.car.advertising.CarApplication;
import com.car.advertising.R;
import com.car.advertising.connection.Contact;
import com.car.advertising.connection.HttpUrl;
import com.car.advertising.db.UserInfoBiz;
import com.car.advertising.db.UserInfoEntiy;
import com.car.advertising.permission.PermissionListener;
import com.car.advertising.permission.PermissionManger;
import com.car.advertising.util.PreferenceUtil;
import com.support.widget.JumpManager;
import com.support.widget.layout.BaseCommActivity;
import com.support.widget.network.OkHttpClientManager;
import com.support.widget.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

import static com.car.advertising.permission.PermissionManger.READ_PHONE_REQUESTCODE;

public class LoadingActivity extends BaseCommActivity {

    private Handler handler = new Handler();
    private UserInfoBiz userInfoBiz;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_loading);
        userInfoBiz = UserInfoBiz.getInstance(LoadingActivity.this);
        PermissionManger.getInstance().requestPermission(this, new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, new String[]{AppOpsManager.OPSTR_READ_PHONE_STATE, AppOpsManager.OPSTR_WRITE_EXTERNAL_STORAGE}, READ_PHONE_REQUESTCODE, new PermissionListener() {
            @Override
            public void granted() {
                gotoMain();
            }
        });
    }

    private void gotoMain() {

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                boolean firstTime = PreferenceUtil.getBoolean("isFirstTime", true);
                if (firstTime) {
                    JumpManager.gotoActivity(LoadingActivity.this, GuideViewActivity.class, true);
                    PreferenceUtil.putBoolean("isFirstTime", false);
                    finish();
                } else {
                    getUserInfo();
                }
            }
        }, 1000);
    }

    private void getUserInfo() {
        String readToken = Contact.readToken();
        if (TextUtils.isEmpty(readToken) && TextUtils.isEmpty(CarApplication.token)) {
            JumpManager.gotoActivity(LoadingActivity.this, MainHomeActivity.class, true);
            finish();
            return;
        }
        List<OkHttpClientManager.Param> params = new ArrayList<>();
        params.add(new OkHttpClientManager.Param("acToken", readToken));
        OkHttpClientManager.postAsyn(HttpUrl.getInstance().URL_039, new OkHttpClientManager.ResultCallback<UserInfoEntiy>() {
            @Override
            public void onError(String request) {
                LogUtil.i(request);
                Contact.readToken();
                JumpManager.gotoActivity(LoadingActivity.this, MainHomeActivity.class, true);
                finish();
            }

            @Override
            public void onResponse(UserInfoEntiy resp) {
                if (resp == null) {
                    Contact.readToken();
                } else {
                    userInfoBiz.saveMerchant(resp);
                }
                JumpManager.gotoActivity(LoadingActivity.this, MainHomeActivity.class, true);
                finish();
            }

            @Override
            public void onFinish() {

            }
        }, params, null);
    }

    @Override
    public String getCN() {
        return null;
    }

    @Override
    public void LogoutCallback(boolean islogin) {

    }

    @Override
    public void onCritClick(View v) {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionManger.getInstance().onRequestPermissionsResult(this, permissions, requestCode, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
